// pagesEnterprise/pages/corporateHome/corporateHome.js
import ajax from '../../../utils/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 详情Id
     */
    detailId: null,
    /**
     * 企业详情 对象
     */
    entity: null,
    /**
     * 轮播图下标
     */
    swiperIndex: 0,
    /**
     * 分享弹出框
     */
    showShare: false
  },
  /**
   * 打开地图
   */
  openMmap() {
    ajax.openMmap(this.data.entity.address)
  },
  /**
   * 打电话
   */
  makePhoneCall () {
    const phone = this.data.entity.phone.split(',');
    ajax.makePhoneCall(phone.shift())
  },
  /**
   * 显示/隐藏分享框
   */
  shareEvent(event) {
    this.setData({ showShare: !(event.detail == 1) })
  },
  /**
   * 接口 - 企业详情
   */
  async getCompanyDetail() {
    const res = await ajax.get('/enterprise/info', { id: this.data.detailId });
    this.setData({ entity: res.data })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onLoad:", options)
    app.globalData.shareId = options.shareId || null
    if (options && options.scene) {
      const entity = ajax.windowSearch(options.scene)
      options.id = entity.id
      app.globalData.shareId = entity.shareId || null
    }
    wx.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'] })
    this.setData({ detailId: options.id })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCompanyDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
     * 朋友圈分享
     */
  onShareTimeline: function () {
    const { entity } = this.data
    const userInfo = wx.getStorageSync('userInfo')
    return {
      title: `${userInfo.nickName}邀请您预览${entity.name}`,
      query: `id=${entity.id}&shareId=${userInfo.id}`,
      imageUrl: entity.indexImage,
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { entity } = this.data
    const userInfo = wx.getStorageSync('userInfo')
    this.shareEvent({ detail: 1 })
    return {
      title: `${userInfo.nickName}邀请您预览${entity.name}`,
      imageUrl: entity.indexImage,
      path: `/pagesEnterprise/pages/corporateHome/corporateHome?id=${entity.id}&shareId=${userInfo.id}`
    }
  }
})